import { clientType, ResultCommon, ResultPagination } from "@/utils/commonType";
import { http } from "@/utils/http";
import { baseUrlApi } from "@/utils/http/utils";



/** 获取系统管理-客户端管理列表 */
export const getClientList = async (tenantId, data?: object) => {
  const result = await http.request<ResultPagination<clientType>>(
    "get",
    baseUrlApi("/client/list/" + tenantId),
    {
      params: data
    }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const changeClientKey = async (clientId: string) => {
  const result = await http.request<ResultCommon<clientType>>(
    "put",
    baseUrlApi("/client/changeKey/" + clientId)
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const changeClientStatus = async (
  clientId: string,
  clientStatus: string
) => {
  let reqUri = "";
  if (clientStatus === "active") {
    reqUri = "/client/active/" + clientId;
  } else {
    reqUri = "/client/archive/" + clientId;
  }
  const result = await http.request<ResultCommon<clientType>>("put", baseUrlApi(reqUri));
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const addClient = async client => {
  const result = await http.request<ResultCommon<clientType>>(
    "post",
    baseUrlApi("/client/add"),
    { data: client }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const updateClient = async (clientId: string, client: clientType) => {
  const result = await http.request<ResultCommon<clientType>>(
    "put",
    baseUrlApi("/client/update/" + clientId),
    { data: client }
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};

export const removeClient = async (clientId: string) => {
  const result = await http.request<ResultCommon<clientType>>(
    "delete",
    baseUrlApi("/client/remove/" + clientId)
  );
  if (result.code != 200) {
    throw new Error(result.message);
  }
  return result;
};
